import QtQuick 2.0
import QtQuick.Window 2.12
import QtQuick.Controls 2.12
Item {

    id: root

    anchors.fill: parent
    property string curId: gPageModel===0 ? gCurDeviceId : gCurNewId

    function refresh(){
        listModel.clear()
        for(let i = 0; i < gDeviceIdList.length; i ++){
            let deviceId = gDeviceIdList[i]

            // 根据设备状态排查设备列表
            let status = gDeviceInfoList[deviceId]["EquStatus"]
            if(gPageModel === 0){
                if(gDeviceFilter !== 5 && gDeviceFilter.toString()!==status.toString()){
                    continue
                }
            }if(gPageModel === 1){
                if(status.toString() === "2" || status.toString() === "4" || status.toString() === "0"){
                    continue
                }
            }

            listModel.append({
                                 "equId": gDeviceInfoList[deviceId]["Id"],
                                 "equName": gDeviceInfoList[deviceId]["EquName"],
                                 "equRoute": gDeviceInfoList[deviceId]["EquConnectType"]===0 ? gDeviceInfoList[deviceId]["Ip"] : gDeviceInfoList[deviceId]["SerialPort"],
                                 "equStatus": gDeviceInfoList[deviceId]["EquStatus"],
                                 "equChecked": curId===deviceId ? true : false
                             })
        }
    }

    Item{   // 用于阴影裁剪
        height: parent.height; width: parent.width+10
        x: -5
        clip: true
        Item{
            x: 5; y: 2
            height: parent.height-4; width: parent.width-10
            ListView{       // 列表
                id: listView
                spacing: 10
                model: listModel
                anchors.fill: parent
                delegate: listDelegate

                ScrollBar.vertical: ScrollBar{}
            }
        }


    }



    ListModel{
        id: listModel
    }
    Component{
        id: listDelegate
        DeviceItem{
            deviceId: equId
            name: equName
            route: equRoute
            status: equStatus
            isChecked: equChecked
            onReleased: {
                if(deviceId !== curId){
                    changeCurId(deviceId)
                }
            }
        }
    }

}
